package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;

/**
 * Created by yun_sheng on 2017/4/28.
 */

public class SpinnerAdapter extends ArrayAdapter {

    private Context context;
    private String[] names;
    private int resource;
    private LayoutInflater infalter;

    public SpinnerAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull Object[] objects) {
        super(context, resource,objects);
        this.context = context;
        this.resource = resource;
        this.names = (String[]) objects;
        infalter = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = null;
        if (textView==null)
            textView = (TextView) infalter.inflate(R.layout.easy_text_layout, null);
        Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.productlist_filter_sel);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth()/2, drawable.getIntrinsicHeight()/2);
        textView.setCompoundDrawables(null, null, drawable, null);
        textView.setText(names[position]);
        return textView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        TextView textView = null;
        LinearLayout linearLayout = null;
        if (linearLayout==null)
            linearLayout = (LinearLayout) infalter.inflate(resource, null);
        textView = (TextView) linearLayout.findViewById(R.id.tvName);
        textView.setText(names[position]);
        return linearLayout;
    }
}
